package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.CancellationPolicy;

public class CancellationPolicyLogic extends AbstractBL {
	public List<CancellationPolicy> GenerateCancellationPolicy(long hotelId)
	{
		List<CancellationPolicy> list = new ArrayList<CancellationPolicy>();
		
		CancellationPolicy cancellationpolicy = new CancellationPolicy();
		cancellationpolicy.setHotelId(hotelId);
		cancellationpolicy.setTitle("CancellationPolicy");
		cancellationpolicy.setDay(0);
		cancellationpolicy.setHours(2);
		cancellationpolicy.setChargeDay(false);
		cancellationpolicy.setDescription("cancel");
		cancellationpolicy.setFix(false);
		cancellationpolicy.setPercentcharge(true);
		cancellationpolicy.setValue(1.00);
		
		CancellationPolicy c = new CancellationPolicy();
		c.setHotelId(hotelId);
		c.setTitle("CancellationPolicy ChargeDate");
		c.setDay(1);
		c.setHours(0);
		c.setChargeDay(false);
		c.setDescription("checkinLate");
		c.setFix(true);
		c.setPercentcharge(false);
		c.setValue(200);
		c.setAvalible(true);
		list.add(cancellationpolicy);
		list.add(c);
		
		return list;
	}
	
	public CancellationPolicy GetCancellationPolicy(long id)
	{
		Key key = KeyFactory.createKey(EntityName.CancellationPolicy, id);
		try {
			Entity lang = datastore.get(key);
			CancellationPolicy c = new CancellationPolicy();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public List<CancellationPolicy> GetAllCancellationPolicy(long hotelId)
	{
		Query q = new Query(EntityName.CancellationPolicy);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avalible",FilterOperator.EQUAL,true);
		
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public CancellationPolicy GetCancellationPolicy(Entity entity)
	{
		CancellationPolicy item = new CancellationPolicy();
		item.setEntity(entity);
		return item;
	}
	
	public List<CancellationPolicy> GetList(List<Entity> entities)
	{
		List<CancellationPolicy> list = new ArrayList<CancellationPolicy>();
		if(entities!=null)
		for(Entity e : entities)
		{
			CancellationPolicy a = new CancellationPolicy();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public CancellationPolicy AddCancellationPolicy(long hotelId, String title,
			String description, boolean percentcharge, boolean fix,
			boolean chargeDay, double value,int Day,int Hours) throws EntityNotFoundException {
			CancellationPolicy c = new CancellationPolicy();
			c.setHotelId(hotelId);
			c.setTitle(title);
			c.setDescription(description);
			c.setPercentcharge(percentcharge);
			c.setFix(fix);
			c.setChargeDay(chargeDay);
			c.setValue(value);
			c.setDay(Day);
			c.setHours(Hours);
			c.setAvalible(true);
			c.setStatus(true);
			
			Key k = datastore.put(c.getEntity());
			c.setId(k.getId());
			c.setKey(k);
		return c;
	}

	public CancellationPolicy EditCancellationPolicy(long cancellationpolicyId,
			String title, String description, boolean percentcharge,
			boolean fix, boolean chargeDay, double value, int Day,int Hours) throws EntityNotFoundException {
		CancellationPolicy c = GetCancellationPolicy(cancellationpolicyId);
			c.setTitle(title);
			c.setDescription(description);
			c.setPercentcharge(percentcharge);
			c.setFix(fix);
			c.setChargeDay(chargeDay);
			c.setValue(value);
			c.setDay(Day);
			c.setDay(Hours);
			
		Key k = datastore.put(c.getEntity());
		c.setId(k.getId());
		c.setKey(k);
		return c;
	}

	public CancellationPolicy DeleteCancellationPolicy(long cancellationpolicyId) throws EntityNotFoundException {
		CancellationPolicy c = GetCancellationPolicy(cancellationpolicyId);
		c.setAvalible(false);
		c.setStatus(false);
		
		Key k = datastore.put(c.getEntity());
		c.setId(k.getId());
		c.setKey(k);
		return c;
	}

	public CancellationPolicy ChangeStatus(long cancellationpolicyId) throws EntityNotFoundException{
		
		CancellationPolicy a = GetCancellationPolicy(cancellationpolicyId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<CancellationPolicy> GetAllCancellationPolicy4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.CancellationPolicy);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avalible",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
}
